package com.zidian.leader.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zidian.leader.R;
import com.zidian.leader.common.util.ColorConstants;
import com.zidian.leader.common.view.ScatterView;
import com.zidian.leader.common.view.StatisticsBean;
import com.zidian.leader.common.view.StatisticsList;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 教师
 * Created by GongCheng on 2016/11/4.
 */

public class TeacherEvaChartAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_TOP = 0;
    private static final int TYPE_ITEM = 1;

    private List<String> evaList;
    private StatisticsList list = new StatisticsList();
    private Context context;

    public TeacherEvaChartAdapter(Context context) {
        this.context = context;
    }

    /**
     * 设置数据
     *
     * @param data 描述List
     */
    public void setData(List<String> data) {
        evaList = data;
        notifyDataSetChanged();
    }

    /** 创建视图**/
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TOP) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_colleague_eva_detail_top, parent, false);
            return new EvaChartTopHolder(view);

        } else if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_teacher_eva_detail, parent, false);
            return new EvaChartItemHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof EvaChartTopHolder) {
            List<StatisticsBean> statisticsBeanList = new ArrayList<>();
            if (evaList == null) {
                return;
            }
            for (int i = 0; i < evaList.size(); i++) {
                statisticsBeanList.add(new StatisticsBean(ColorConstants.CHART_COLORS[i], 1));
            }
            list.setList(statisticsBeanList);
            list.setCount(statisticsBeanList.size());
            ((EvaChartTopHolder) holder).scatterView.setData(list);

        } else if (holder instanceof EvaChartItemHolder) {
            ((EvaChartItemHolder) holder).evaluateScoreTv.setText(evaList.get(position - 1)+ "");
            ((EvaChartItemHolder) holder).evaluateTypeTv.setText(evaList.get(position - 1));
            ((EvaChartItemHolder) holder).evaluateColorIv.setBackgroundColor(ColorConstants.CHART_COLORS[position - 1]);
        }

    }

    @Override
    public int getItemCount() {
        if (evaList == null) {
            return 0;
        }
        return evaList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        } else {
            return TYPE_ITEM;
        }
    }

    /**
     * Top BarChartViewHolder
     */
    class EvaChartTopHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.scatterView)
        ScatterView scatterView;

        EvaChartTopHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * Item BarChartViewHolder
     */
    class EvaChartItemHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.evaluate_type_tv)
        TextView evaluateTypeTv;
        @Bind(R.id.evaluate_score_tv)
        TextView evaluateScoreTv;
        @Bind(R.id.evaluate_color_iv)
        ImageView evaluateColorIv;

        EvaChartItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
